Skip to content

fix(network): support ChangeL2NetworkVlanId for ZNS networks#3723

Closed
ZStack-Robot wants to merge 1 commit intofeature-5.5.12-znsfrom
sync/shixin.ruan/shixin.ruan-zcf-2074@@2
Closed

fix(network): support ChangeL2NetworkVlanId for ZNS networks#3723
ZStack-Robot wants to merge 1 commit intofeature-5.5.12-znsfrom
sync/shixin.ruan/shixin.ruan-zcf-2074@@2

Conversation

@ZStack-Robot
Copy link
Copy Markdown
Collaborator

When type param is null in APIChangeL2NetworkVlanIdMsg, default to current
network type to prevent NPE in interceptors and handlers. Add null-safe
type check in VxlanNetworkCheckerImpl. Add ZNS error codes 10011-10015.

Resolves: ZCF-2074

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com

sync from gitlab !9593

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 10, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

该合并请求为 L2 网络 VLAN ID 变更增加了空值/类型归一化检查,修复了 VxLAN 类型比较的 NPE 风险,新增了 L2 网络扩展点的 beforeUpdate 回调及差异化异常处理,并在错误码中添加了两个常量(10016、10017)。

Changes

Cohort / File(s) Summary
L2 网络拦截器
network/src/main/java/org/zstack/network/l2/L2NetworkApiInterceptor.java
validate(APIChangeL2NetworkVlanIdMsg) 中归一化/修剪 msg.getType(),当为空时使用现有 L2 类型,基于计算的布尔值(如 targetIsVlan/targetIsNoVlan/targetIsGeneve)调整后续验证与拒绝分支。
VxLAN 空指针防护
plugin/vxlan/src/main/java/org/zstack/network/l2/vxlan/vxlanNetworkPool/VxlanNetworkCheckerImpl.java
将类型比较改为以常量调用 equals!VxlanNetworkConstant.VXLAN_NETWORK_TYPE.equals(msg.getType()))以避免对 msg.getType() 的 NPE。
扩展点回调新增
network/src/main/java/org/zstack/network/l2/L2NetworkExtensionPointEmitter.java
新增 public void beforeUpdate(L2NetworkInventory inv):遍历 updateExtensions 调用 beforeChangeL2NetworkVlanId(inv),遇 RuntimeException 重新抛出,其他异常记录为警告;afterUpdate 未变更。
主机助手微调
network/src/main/java/org/zstack/network/l2/L2NetworkHostHelper.java
移除未使用的 Collectors 导入;getHostsByL2NetworkAttachedClusterattachedClusterUuids 为空时立即返回空集合,避免使用空/空列表查询。
错误码常量
utils/src/main/java/org/zstack/utils/clouderrorcode/CloudOperationsErrorCode.java
新增两个错误码常量 ORG_ZSTACK_NETWORK_ZNS_10016ORG_ZSTACK_NETWORK_ZNS_10017 并更新相关注释映射说明。

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 我在代码林间轻嗅春风,补上空白的小洞,
把脆弱的等号换成稳稳的拥抱,免去惊慌,
在更新前我敲门请安,异常悄声记下,
新的编号静静挂上枝头,网络在月光下跳舞。

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed PR标题遵循[scope]: 格式,长度为60字符,明确概括了PR的主要变更内容(支持ZNS网络的ChangeL2NetworkVlanId功能)。
Description check ✅ Passed PR描述清晰详尽,完整说明了变更的主要内容、目的和对应的issues,与代码变更高度相关。

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch sync/shixin.ruan/shixin.ruan-zcf-2074@@2

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
network/src/main/java/org/zstack/network/l2/L2NetworkApiInterceptor.java (1)

147-150: 建议在默认回填前对 type 做 trim 归一化

Line 147-150 目前只处理 null。若传入空白字符串(如 " "),不会回填默认类型,可能绕过后续类型分支校验。建议在这里先 trimToNull 再回填。

建议修改
-        // When type is not specified, default to the current network type
-        if (msg.getType() == null) {
-            msg.setType(l2.getType());
-        }
+        // When type is not specified, default to the current network type
+        msg.setType(StringUtils.trimToNull(msg.getType()));
+        if (msg.getType() == null) {
+            msg.setType(l2.getType());
+        }

As per coding guidelines "注意检查来自 Message 的参数是否做过 trim,用户可能在浏览器上复制粘贴的数据带有空格、换行符等"。

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@network/src/main/java/org/zstack/network/l2/L2NetworkApiInterceptor.java`
around lines 147 - 150, The code in L2NetworkApiInterceptor currently only
checks msg.getType() for null before filling the default; update the logic to
normalize/trim the incoming type first (e.g., treat blank or whitespace-only
strings as null) by using a trim-to-null step on msg.getType() and then call
msg.setType(l2.getType()) when the trimmed value is null/empty; locate the
handling around msg.getType()/msg.setType() in L2NetworkApiInterceptor and apply
the trim-to-null normalization prior to the default-backfill so blank strings
don't bypass type validation.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@network/src/main/java/org/zstack/network/l2/L2NetworkApiInterceptor.java`:
- Around line 147-150: The code in L2NetworkApiInterceptor currently only checks
msg.getType() for null before filling the default; update the logic to
normalize/trim the incoming type first (e.g., treat blank or whitespace-only
strings as null) by using a trim-to-null step on msg.getType() and then call
msg.setType(l2.getType()) when the trimmed value is null/empty; locate the
handling around msg.getType()/msg.setType() in L2NetworkApiInterceptor and apply
the trim-to-null normalization prior to the default-backfill so blank strings
don't bypass type validation.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: http://open.zstack.ai:20001/code-reviews/zstack-cloud.yaml (via .coderabbit.yaml)

Review profile: CHILL

Plan: Pro

Run ID: 9dfccf31-b657-456c-9192-2285114fdae7

📥 Commits

Reviewing files that changed from the base of the PR and between 17d06ab and 9ad4368.

📒 Files selected for processing (3)
  • network/src/main/java/org/zstack/network/l2/L2NetworkApiInterceptor.java
  • plugin/vxlan/src/main/java/org/zstack/network/l2/vxlan/vxlanNetworkPool/VxlanNetworkCheckerImpl.java
  • utils/src/main/java/org/zstack/utils/clouderrorcode/CloudOperationsErrorCode.java

@MatheMatrix MatheMatrix force-pushed the sync/shixin.ruan/shixin.ruan-zcf-2074@@2 branch 2 times, most recently from 318d81c to ecf5fe8 Compare April 14, 2026 02:50
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@network/src/main/java/org/zstack/network/l2/L2NetworkApiInterceptor.java`:
- Around line 149-152: Normalize and validate the incoming type on
L2NetworkApiInterceptor: instead of only checking msg.getType() == null, trim
the value from msg.getType(), treat empty/blank strings as null and set to
l2.getType() via msg.setType(), and then explicitly reject any unsupported
values (not matching expected enums/types used by the existing branches) with a
clear error; update the logic around msg.getType()/msg.setType() so subsequent
branches that handle VLAN and other type-specific validation (the branches
referenced after this code) cannot be bypassed by blank or unknown type strings.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: http://open.zstack.ai:20001/code-reviews/zstack-cloud.yaml (via .coderabbit.yaml)

Review profile: CHILL

Plan: Pro

Run ID: 2ba1c2f9-9b02-4d7b-998c-4f2809ec0626

📥 Commits

Reviewing files that changed from the base of the PR and between 318d81c and ecf5fe8.

📒 Files selected for processing (4)
  • network/src/main/java/org/zstack/network/l2/L2NetworkApiInterceptor.java
  • network/src/main/java/org/zstack/network/l2/L2NetworkExtensionPointEmitter.java
  • plugin/vxlan/src/main/java/org/zstack/network/l2/vxlan/vxlanNetworkPool/VxlanNetworkCheckerImpl.java
  • utils/src/main/java/org/zstack/utils/clouderrorcode/CloudOperationsErrorCode.java
✅ Files skipped from review due to trivial changes (1)
  • utils/src/main/java/org/zstack/utils/clouderrorcode/CloudOperationsErrorCode.java
🚧 Files skipped from review as they are similar to previous changes (1)
  • plugin/vxlan/src/main/java/org/zstack/network/l2/vxlan/vxlanNetworkPool/VxlanNetworkCheckerImpl.java

Comment thread network/src/main/java/org/zstack/network/l2/L2NetworkApiInterceptor.java Outdated
@MatheMatrix MatheMatrix force-pushed the sync/shixin.ruan/shixin.ruan-zcf-2074@@2 branch 2 times, most recently from 1522f32 to fa787f4 Compare April 14, 2026 06:44
Handle blank or omitted type in APIChangeL2NetworkVlanIdMsg by
normalizing to the current L2 type before validation checks.

Resolves: ZSTAC-2074

Change-Id: Icf960d0766b726047d8613305042cfa14e120c61
@MatheMatrix MatheMatrix force-pushed the sync/shixin.ruan/shixin.ruan-zcf-2074@@2 branch from fa787f4 to e8bc99f Compare April 14, 2026 07:54
@MatheMatrix MatheMatrix deleted the sync/shixin.ruan/shixin.ruan-zcf-2074@@2 branch April 14, 2026 09:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants